package com.xs.tool.common.utils.vali;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashSet;
import java.util.Set;

/**
 * 实体类键值属性检测
 *
 * @author xs
 * @date 20210116
 */
public class ValidatorUtil {
    private static Logger logger = LoggerFactory.getLogger(ValidatorUtil.class);

    public static Validator getInstance() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        return factory.getValidator();
    }

    /**
     * 检查对象
     *
     * @param object
     * @param <T>
     * @return
     */
    public static <T> boolean isObjValid(T object) {
        boolean result = true;
        if (object != null) {
            Set<ConstraintViolation<T>> constraintViolations = getInstance().validate(object);
            if (constraintViolations.size() > 0) {
                result = false;
                for (ConstraintViolation violation : constraintViolations) {
                    logger.info("filed={}, msg={}, currentValue={}.",
                            violation.getPropertyPath().toString(),
                            violation.getMessage(),
                            violation.getInvalidValue()
                    );
                }
            }
        }
        return result;
    }

    /**
     * 检查实体对象
     *
     * @param object
     * @param <T>
     * @return
     */
    public static <T> Set<ValidateMessage> valid(T object) {
        Set<ValidateMessage> validateMessageSet = new HashSet<ValidateMessage>();
        if (object != null) {
            Set<ConstraintViolation<T>> constraintViolations = getInstance().validate(object);
            if (constraintViolations.size() > 0) {
                for (ConstraintViolation violation : constraintViolations) {
                    String path = "";//信息
                    String msg = "";//信息
                    String val = "";//信息
                    if (null != violation.getPropertyPath()) {
                        path = violation.getPropertyPath().toString();
                    }
                    if (null != violation.getMessage()) {
                        msg = violation.getMessage().toString();
                    }
                    if (null != violation.getInvalidValue()) {
                        val = violation.getInvalidValue().toString();
                    }
                    ValidateMessage message = new ValidateMessage(path, msg, val);
                    validateMessageSet.add(message);
                }
            }
        }
        return validateMessageSet;
    }

    /**
     * 检查实体对象
     *
     * @param validateMessageSet 校验结果
     * @return
     */
    public static String validStr(Set<ValidateMessage> validateMessageSet) {
        if (null != validateMessageSet && validateMessageSet.size() > 0) {
            return null;
        }
        try {
            StringBuilder stringBuilder = new StringBuilder();
            for(ValidateMessage validateMessage:validateMessageSet){
                stringBuilder.append(validateMessage.getMessage()+",");
            }
            String result = stringBuilder.toString();
            result = result.substring(0,result.length()-1);
            return result;
        }catch (Exception e){
            logger.error(e.getMessage());
        }
        return null;
    }
}
